package org.eclipse.equinox.spring.osgi;
/**
 * This file is part of org.springframework.equinox.deployer. 
 * Copyright (C) olivier.evalet@genebio.com and genebio team
 * 
 * org.springframework.equinox.deployer is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 * 
 * org.springframework.equinox.deployer is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * org.springframework.equinox.deployer. If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.IOException;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Map;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.springframework.beans.BeanUtils;

public class SpringServlet implements Servlet {
	private Servlet adaptedActionServlet;
	private Hashtable<String, String> initConfig=null;
	private String name;
	
	public SpringServlet(String clazz)throws ServletException {
		try {
			adaptedActionServlet = (Servlet) BeanUtils.instantiateClass(Class.forName(clazz));
		} catch (Exception e) {
			ServletException s=new ServletException(e.getMessage());
			s.initCause(e);
			throw s;
		}
	}
	public SpringServlet(Servlet who)throws ServletException {
		adaptedActionServlet = (Servlet) who;
	}	
	public void setInitConfig(Map<String,String> config){
		this.initConfig=new Hashtable<String, String>(config);
//		for (String key:config.keySet()){
//			this.initConfig.put(key, config.get(key));
//		}
	}
	
	public Map<String, String> getInitConfig(){
		return initConfig;
	}
	
	public void setUri(String name){
		if (name.indexOf('/')!=0)
			name="/"+name;
		this.name=name;
	}

	public String getUri(){
		return name;
	}
	
	@Override
	public void destroy() {
		adaptedActionServlet.destroy();
	}

	@Override
	public ServletConfig getServletConfig() {
		return adaptedActionServlet.getServletConfig();
	}

	@Override
	public String getServletInfo() {
		return adaptedActionServlet.getServletInfo();
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		adaptedActionServlet.init(config);
	}

	@Override
	public void service(ServletRequest req, ServletResponse res)
			throws ServletException, IOException {
		adaptedActionServlet.service(req, res);
	}

}
